package com.cslg.ac.demo.controller.like;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import com.cslg.ac.demo.entity.user.User;
import com.cslg.ac.demo.util.communicate.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cslg.ac.demo.constant.LikeType;
import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.service.like.LikeService;

import javax.servlet.http.HttpSession;

@Controller
public class LikeController {

	@Autowired
	private LikeService likeService;
	
	@RequestMapping(value="/like/add")
	public void add(@RequestParam int targetId, HttpSession session, PrintWriter out)
	{
		User user = (User)session.getAttribute("user");
		int id = user.getId();
		if(targetId <= 0 || user.getId() == targetId)
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int code = 0;
		try
		{
			code = likeService.add(id, LikeType.USER, targetId);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		ResponseUtil.responseCode(out, code);
	}
	
	@RequestMapping(value="/like/delete")
	public void delete( @RequestParam int targetId, HttpSession session ,PrintWriter out)
	{
		User user = (User) session.getAttribute("user");
		int id = user.getId();
		if(targetId <= 0)
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int code = 0;
		try
		{
			code = likeService.delete(id, LikeType.USER, targetId);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		ResponseUtil.responseCode(out, code);
	}
	
	/**
	 * 获取某人关注的所有人
	 * @param id
	 * @param session
	 * @param out
	 */
	@RequestMapping(value="/like/get")
	public void get(@RequestParam int id,HttpSession session,PrintWriter out)
	{
		if(id <= 0){
			ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int[] like;
		try
		{
			like = likeService.getAll(id, LikeType.USER);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("like", like);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}
	
	/**
	 * 获取某人的所有粉丝
	 */
	@RequestMapping(value="/like/follow")
	public void follow(@RequestParam int id, PrintWriter out,HttpSession session)
	{
		if(id < 0)
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int[] follow = null;
		try
		{
			follow = likeService.follow(id, LikeType.USER);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("follow", follow);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}
	
}
